<template>
  <div class="card w-100% h-100%">
    <el-row>
      <el-col :span="24">
        <div class="m-b-5">
          <div>
            <el-button v-auth="'add'" type="success" @click="handleAdd()" :icon="Plus">新增</el-button>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form v-auth="'search'" :inline="true" :model="searchForm">
          <el-form-item label="角色编码" prop="roleCode">
            <el-input v-model="searchForm.roleCode" placeholder="请输入角色编码" clearable @keyup.enter="search()"></el-input>
          </el-form-item>
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="searchForm.roleName" placeholder="请输入角色名称" clearable @keyup.enter="search()"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search()" :icon="Search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column label="角色编码" prop="roleCode" />
          <el-table-column label="角色名称" prop="roleName" />
          <!-- <el-table-column label="备注" prop="remark" />  -->
          <el-table-column v-auth="['edit', 'delete', 'roleConfig']" label="操作" width="340">
            <template #default="scope">
              <div class="flex justify-center">
                <el-button v-auth="'roleConfig'" @click="configRole(scope.row)">配置权限</el-button>
                <el-button v-auth="'edit'" type="primary" @click="handleEdit(scope.row)":icon="Edit">编辑</el-button>
                <el-button v-auth="'delete'" type="danger" @click="handleDelete(scope.row.roleId)" :icon="Delete">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row class="mt-5">
      <el-pagination
        v-model:current-page="searchForm.current"
        v-model:page-size="searchForm.size"
        layout="total,sizes,prev, pager, next,jumper"
        :page-sizes="[10, 50, 100, 200, 500]"
        :total="total"
        @size-change="handleSizeChange"
        :background="true"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <!-- 权限配置 -->
    <roleConfig ref="roleConfigRef"></roleConfig>
    <!-- 新增修改角色-->
    <editRole @confirm="search()" ref="editRoleRef"></editRole>
  </div>
</template>
<script setup>
defineOptions({
  name: 'role',
});
import { ref, onMounted } from "vue";
import { getRoleList, deleteRole } from "@/api/modules/role";
import roleConfig from "../common/roleConfig.vue";
import editRole from "./editRole.vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { Delete, Edit, Search, Plus } from '@element-plus/icons-vue'
const searchForm = ref({
  roleCode: "",
  roleName: "",
  size: 10,
  current: 1
});
const search = () => {
  getRoleList(searchForm.value).then(res => {
    tableData.value = res.data.others;
    total.value = res.data.total;
  });
};
const tableData = ref([]);
const total = ref(0);
const handleSizeChange = val => {
  searchForm.value.size = val;
  search();
};
const handleCurrentChange = val => {
  searchForm.value.current = val;
  search();
};
const roleConfigRef = ref(null);
const configRole = row => {
  roleConfigRef.value.openDialog(row.roleId);
};
const editRoleRef = ref(null);
const handleAdd = () => {
  editRoleRef.value.openDialog();
};
const handleEdit = row => {
  editRoleRef.value.openDialog(row.roleId);
};
const handleDelete = id => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteRole(id).then(res => {
      if (res.code === 200) {
        ElMessage.success("删除成功！");
        search();
      }
    });
  });
};
onMounted(() => {
  search();
});
</script>
